$(function(){
    getBannerList();
})
function getBannerList(){
    var swiperBanner;
    $.post(ctx + "banner/globalbanner/list", {ctx:ctx}, function (result) {
        if(result.code == "0"){
            if(result.userId == null){
                //匿名访问时banner铺满
                $('.banner-slide img ').css("width","1200px");
                $('.banner-list').css("width","1200px");
                $('.banner-point').css("left","930px");
            }else{
                initRecentCourse();
            }
            if(result.bannerList != null && result.bannerList.length > 0){
                //获取图片
                var html = '';
                $.each(result.bannerList, function (index, item) {
                    var clickEvent = '';
                    if(item.bannerType == "2" && item.url != null && item.url != '' && item.url != 'null'){
                        clickEvent = 'onclick="toBannerUrl(\''+item.url+'\')" style="cursor:pointer;"';
                    }else if(item.bannerType == "3" && item.dataId != null){
                        clickEvent = 'onclick="toCourseDetail(\''+item.dataId+'\')" style="cursor:pointer;"';
                    }else if(item.bannerType == "4" && item.dataId != null){
                        clickEvent = 'onclick="toExamStart(\''+item.dataId+'\')" style="cursor:pointer;"';
                    }else if(item.bannerType == "5" && item.dataId != null){
                        clickEvent = 'onclick="toStudyPlanDetail(\''+item.dataId+'\')" style="cursor:pointer;"';
                    }
                    html += '<div class="swiper-slide"'+ clickEvent +' b-color="'+ item.color +'">' +
                        '<img src="'+ pathToUrl(item.imageUrl) +'">' +
                        '</div>';
                });
                $('.banner-list .swiper-wrapper').html(html);

                if(result.bannerList.length > 1){
                    //轮播
                    swiperBanner = new Swiper('.banner-list', {
                        pagination: '.banner-list .swiper-pagination',
                        nextButton: '.banner-list .swiper-button-next',
                        prevButton: '.banner-list .swiper-button-prev',
                        autoplayDisableOnInteraction : false,
                        slidesPerView: 1,
                        paginationClickable: true,
                        loop: true,
                        autoplay: 3000,//可选选项，自动滑动
                        runCallbacksOnInit: false,
                        //图片过渡事件
                        onSetTransition: function(item,transiton){
                            updBackgroundColor(item.activeIndex);
                        }
                    });
                }else{
                    //轮播
                    swiperBanner = new Swiper('.banner-list', {
                        autoplayDisableOnInteraction : false,
                        slidesPerView: 1,
                        paginationClickable: false,
                        loop: false,
                        autoplay: false,//可选选项，自动滑动
                        runCallbacksOnInit: false
                    });
                    //补色
                    updBackgroundColor(0);
                    //只有一张banner时隐藏标点和翻页按钮
                    $(".banner-point").hide();
                    $('.banner-slide .swiper-button-white').hide();
                }
            }
        } else {
            console.log(result.msg);
        }
    });
}
//修改两侧背景色
function updBackgroundColor(index){
    var color = document.getElementsByClassName('banner-swiper-wrapper')[0].children[index].getAttribute('b-color');
    document.getElementsByClassName('banner-slide')[0].style.background = color;
}

//点击banner进行跳转
function toBannerUrl(url){
    if(url!=null && url != '' && url != 'null'){
        window.open(url);
    }
}